package imageexample;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;



public class ShowImage extends Panel implements MouseListener,MouseMotionListener {
  private BufferedImage  image,imageaux,mask;
  private int w;
  private int h;
  public boolean isSet=false;
  private int pcid=0;
  private String type;
  private int xvalue;
  private int yvalue;
  private ArrayList puntosdecontrol = new ArrayList(); 
  
  
  public void saveImage()
  {
      imageaux=new BufferedImage(image.getWidth(),image.getHeight(),image.getType());
      for(int i=0;i<image.getHeight();i+=1)
          for(int j=0;j<image.getWidth();j+=1)
          {
              imageaux.setRGB(j, i, image.getRGB(j, i));
          }
  }
  public BufferedImage getBackImage()
  {
      return imageaux;
  }
  
  public String getType()
  {
      return type;
  }
  public BufferedImage getImage()
  {
      return image;
  }
  
  public BufferedImage getMask()
  {
      return mask;
  }
  
  public void setType(String str)
  {
      this.type=str;
  }
  
  public int getPcId()
  {
      return this.pcid;
  }
  
  public void setMask(BufferedImage img)
  {
      
   this.mask = new BufferedImage(img.getWidth(),img.getHeight(),img.getType());
   for(int i=0;i<img.getHeight();i+=1)
       for(int j=0;j<img.getWidth();j+=1)
       {
           this.mask.getRaster().setSample(j, i, 0, 255);
       }
  }
  
  public void setImage(BufferedImage img)
  {

            image.flush();
            image = img;      
            w = image.getWidth();
            h = image.getHeight();
            puntosdecontrol.clear();
            repaint();
         
  }
  
 
  
  public ArrayList getPuntosdeControl()
  {
      return this.puntosdecontrol;
  }
  public ShowImage(String str) {
  try {
 image = ImageIO.read(new File(str));
  w=image.getWidth();
  h=image.getHeight();
  } catch (IOException ie) {
  System.out.println("Error:"+ie.getMessage());
  }
  }
  
   public ShowImage(BufferedImage img) {
  image = img;
  w=img.getWidth();
  h=img.getHeight();
  }

            
  @Override
  public void paint(Graphics g) {
      if("componer".equals(this.type))
      {
          for(int i=0;i<10;i+=1)
          {
              for(int j=0;j<10;j+=1)
              {
                  if(xvalue+i>0 && xvalue+i<this.image.getWidth() && yvalue+j>0 && yvalue+j<this.image.getHeight())
                  {
                      mask.setRGB(xvalue+i, yvalue+j, 0);
                      if(this.isSet==true)
                      this.image.setRGB(xvalue+i, yvalue+j, 0);
                  }
              }
          }
          
              
              
      }
  g.drawImage( image, 0, 0, null);
  }
    
    @Override
     public Dimension getPreferredSize() {
         return new Dimension(w, h);
     }
    
    

  static public void main(String args[]) throws
Exception {
  
  JFrame frame = new JFrame("Display image");
  JButton pc = new JButton("puntos de control");
  final ShowImage img = new ShowImage("C:/Users/rodrigo/Documents/NetBeansProjects/ImageExample/strawberry.jpg");
  final ShowImage img2 = new ShowImage("C:/Users/rodrigo/Documents/NetBeansProjects/ImageExample/strawberry2.jpg");
  
  frame.setLayout(new GridLayout(2,2));
  
  pc.addActionListener(new ActionListener() {
            @Override
        public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand()=="puntos de control")
                {
                    int i=0;
                    while(i+1<img.pcid)
                    {
                        System.out.println("punto de control imagen 1: "+(int)i/2+" coord x" + Integer.parseInt(img.puntosdecontrol.get(i).toString()) +" Coord y"+img.puntosdecontrol.get(i+1));
                        i+=2;
                    }
                    
                    i=0;
                    while(i+1<img2.pcid)
                    {
                        System.out.println("punto de control imagen 2:"+(int)i/2+" coord x" + img2.puntosdecontrol.get(i) +" Coord y"+img2.puntosdecontrol.get(i+1));
                        i+=2;
                    }
                }
        }
  });
  frame.add(img);
  frame.add(img2);
  frame.add(pc);
  
  frame.setSize(500, 500);
  img.addMouseListener(img);
  img2.addMouseListener(img2);
  frame.setVisible(true);
  }

    @Override
    public void mouseClicked(MouseEvent e) {
       
        if("registro".equals(this.type))
        {
        if ((e.getX() < this.w) && (e.getY()< this.h))
                    {
                    System.out.println(e.getX() + " " + e.getY());
                    this.image.setRGB(e.getX(),e.getY() , Color.YELLOW.getRGB());
                    this.puntosdecontrol.add(e.getX());
                    this.puntosdecontrol.add(e.getY());
                    this.pcid+=2;
                    repaint();
                    }
        }
        
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
        
        
       
       
       
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
                
        if(this.type=="componer")
        {
            
        
                xvalue = e.getX();

                yvalue = e.getY();

               repaint();
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
     //   throw new UnsupportedOperationException("Not supported yet.");
    }
}